package com.kgc.xsConsumer.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.xs.pojo.SalesOrders;
import com.kgc.xsConsumer.feign.SalesOrdersFeign;
import com.kgc.xsConsumer.feign.SalesQuotesFeign;
import com.kgc.xsConsumer.feign.XSWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/salesOrder")
@CrossOrigin
public class SalesOrdersController  {

    private Logger logger = LoggerFactory.getLogger(SalesOrdersController.class);

    @Autowired
    private SalesOrdersFeign salesOrdersFeign;

    @Autowired
    private XSWorkFlowFeign xsWorkFlowFeign;

    @Autowired
    private SalesQuotesFeign salesQuotesFeign;



    @RequestMapping("/selectSalesOrderByPage")
    public ResponseMsg selectSalesOrderByPage(@RequestBody Map<String,Object> map){
        logger.info("SalesOrdersController selectSalesQuotesByPage params,{}",map.toString());
        ResponseMsg responseMsg=salesOrdersFeign.selectSalesOrdersByPage(map);
        logger.debug("SalesOrdersController selectSalesQuotesByPage result:{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/insertSalesOrder")
    public  ResponseMsg  insertSalesOrder(@RequestBody Map<String,Object> map){
        logger.info("SalesOrdersController insertSalesQuotes params,{}",map.toString());
        ResponseMsg responseMsg=salesOrdersFeign.insertSalesOrders(map);
        logger.debug("SalesOrdersController insertSalesQuotes result:{}",responseMsg);
        ObjectMapper mapper = new ObjectMapper();

        // 转换 SalesOrder 对象
        SalesOrders order = mapper.convertValue(
                map.get("salesOrders"),
                SalesOrders.class
        );

        Map<String,Object> variables = new HashMap<>();
        variables.put("code",order.getDocCode());//单据编号
        variables.put("type",order.getTypeId());//流程类型
        //创建一个流程实例
        xsWorkFlowFeign.startProcess(variables);
        return responseMsg;
    }

    @RequestMapping("/deleteSalesOrders")
    public  ResponseMsg  deleteSalesOrders(@RequestBody Map<String,Object> map){
        logger.info("SalesOrdersController deleteSalesOrders params,{}",map.toString());
        ResponseMsg responseMsg=salesOrdersFeign.deleteSalesOrders(map);
        logger.debug("SalesOrdersController deleteSalesOrdersDetail result:{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesOrdersByCode")
    public  ResponseMsg selectSalesOrdersByCode(String docCode){
        logger.info("SalesOrdersController selectSalesOrdersByCode params,{}",docCode);
        ResponseMsg responseMsg=salesOrdersFeign.selectSalesOrdersByCode(docCode);
        logger.debug("SalesOrdersController selectSalesOrdersByCode result:{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesOrdersDetailByCode")
    public  ResponseMsg selectSalesOrdersDetailByCode(String xsOrderCode){
        logger.info("SalesOrdersController selectSalesOrdersDetailByCode params,{}",xsOrderCode);
        ResponseMsg responseMsg=salesOrdersFeign.selectSalesOrdersDetailByCode(xsOrderCode);
        logger.debug("SalesOrdersController selectSalesOrdersDetailByCode result:{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/check")
    public  ResponseMsg check(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = new ResponseMsg();
        logger.info("SalesOrdersController check params,{}",map.toString());
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("docCode"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
      com.kgc.activiti.util.ResponseMsg msg=  xsWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        return responseMsg;
    }

    @RequestMapping("/updateSalesOrders")
    public ResponseMsg updateSalesOrders(@RequestBody Map<String,Object> map){
        logger.info("SalesOrdersController updateSalesOrders params,{}",map.toString());
        ResponseMsg responseMsg=salesOrdersFeign.updateSalesOrders(map);
        logger.debug("SalesOrdersController updateSalesOrders result:{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/checkStatus")
    public  ResponseMsg checkStatus(@RequestBody Map<String,Object> map){
        logger.info("SalesOrdersController checkStatus params,{}",map.toString());
        ResponseMsg responseMsg=salesOrdersFeign.checkSalesOrdersById(map);
        logger.debug("SalesOrdersController checkStatus result:{}",responseMsg);
        return responseMsg;
    }


}
